package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrdersettingDto;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Orders;
import com.itheima.health.service.OrdersettingService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDto ordersettingDto;
    //文件上传
    @Override
    public void upload(MultipartFile excelFile) throws IOException {
        //读取Excel文件
        InputStream inputStream = excelFile.getInputStream();
        //创建一个工excel文件
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        //打开一个工作页
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取最后的行数
        int lastRowNum = sheet.getLastRowNum();
        int rowNum = 1;
        while (rowNum<=lastRowNum) {
            XSSFRow sheetRow = sheet.getRow(rowNum);
            XSSFCell cell1 = sheetRow.getCell(0);
            Date orderDate = cell1.getDateCellValue();
            XSSFCell cell2 = sheetRow.getCell(1);
            int number = (int) cell2.getNumericCellValue();
            ordersettingDto.insert(orderDate, number);
            rowNum++;
        }
    }
    //预约信息展示
    @Override
    public Result selectOrderSettingByMonth(String year, String month) {
        //将参数转换成对应的属性
        int years = Integer.parseInt(year);
        int months = Integer.parseInt(month);
        //获取当前时间
        LocalDate now = LocalDate.now();
        //获取当月的第一天
        LocalDate oneDayOfMonth = LocalDate.of(years, months, 1);
        //获取当月的最后一天
        LocalDate endDayOfMonth = oneDayOfMonth.with(TemporalAdjusters.lastDayOfMonth());
        //时间日期的集合
        List<OrderSetting> list = new ArrayList<>();
        //判断这个月的第一个天是不是在现在之后
        if(oneDayOfMonth.isAfter(now)){
             list =ordersettingDto.selectOrderSettingByMonth(oneDayOfMonth,endDayOfMonth);
        }else{
             list=ordersettingDto.selectOrderSettingByMonth(now,endDayOfMonth) ;
        }
        //返回结果的集合
        List<Orders> orders = new ArrayList<>();
        //循环集合
        for (OrderSetting orderSetting : list) {
            Date date = orderSetting.getOrderDate();
            //获取当前时间
            Calendar calendar = Calendar.getInstance();
            //setTime()该方法采用Date类型的一个参数dt表示要设置的给定日期。
            calendar.setTime(date);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            //创建一个对象
            Orders Orders = new Orders();
            //将 天 存入对象中
            Orders.setDate(day);
            //将预约人数存入对象中
            Orders.setNumber(orderSetting.getNumber());
            //将以预约的人数存入对象中
            Orders.setReservations(orderSetting.getReservations());
            //将对象添加到集合中
            orders.add(Orders);
        }
        //返回封装对象
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS,orders);
    }
    //可以单独设置预约人数
    @Override
    public Result editNumberByDate(OrderSetting orderSetting) {
        //从参数中获取日期
        Date orderDate = orderSetting.getOrderDate();
        //从参数中获取预约人数
        int number = orderSetting.getNumber();
        //调用Mapper修改预约人数
        ordersettingDto.editNumberByDate(orderDate,number);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
}